package com.lujieni.myredis.redis;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Package: com.lujieni.myredis.redis
 * @ClassName: CacheHandler
 * @Author: lujieni
 * @Description: 通用缓存处理类
 * @Date: 2021-02-19 16:42
 * @Version: 1.0
 */
public class CacheHandler<P extends ICacheProvider<String,V>,S extends ICacheStorage<String,V>,V> implements ICache<String,V>{

    @Autowired
    private P cacheProvider;

    @Autowired
    private S cacheStorage;


    @Override
    public V get(String key) {
        V result = cacheStorage.get(key);
        if(result == null){ //redis中没有从db中拿
            result = cacheProvider.get(key);
            if(result == null){ //db中也没有
                return null;
            }else{
                cacheStorage.set(key,result,100);//存入redis中,ttl为100s
                return result;
            }
        }else{
            return result;
        }
    }

}